//
// Copyright (c) 2009-2011, the Open Video Player authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are 
// met:
//
//    * Redistributions of source code must retain the above copyright 
//		notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above 
//		copyright notice, this list of conditions and the following 
//		disclaimer in the documentation and/or other materials provided 
//		with the distribution.
//    * Neither the name of the openvideoplayer.org nor the names of its 
//		contributors may be used to endorse or promote products derived 
//		from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
package org.openvideoplayer.samples.presenter
{
	import flash.events.NetStatusEvent;
	
	import org.openvideoplayer.components.ui.controlbar.event.ControlBarPropertyChangeEvent;//add by xys
	import org.openvideoplayer.components.ui.controlbar.view.MuteButton;
	import org.openvideoplayer.components.ui.shared.event.ControlEvent;
	import org.openvideoplayer.events.OvpEvent;
	import org.openvideoplayer.net.OvpConnection;
	import org.openvideoplayer.net.OvpNetStream;
	import org.openvideoplayer.samples.view.BaseVideoView;
	
	public class FMSTrickModeControlBarPresenter extends StreamingControlBarPresenter
	{
		
		private var trickModeActive:Boolean;
		private var muteButton:MuteButton;
		private var restoreVolumeLevel:Number;
		
		public function FMSTrickModeControlBarPresenter(view:BaseVideoView, netStream:OvpNetStream, netConnection:OvpConnection, streamName:String)
		{
			super(view, netStream, netConnection, streamName);
		}
		
		override protected function addControlBarListeners():void
		{
			super.addControlBarListeners();
			controlBar.addEventListener(ControlEvent.SLOW_MOTION_FORWARD_DOWN, onSlowMotionForwardDown);
			controlBar.addEventListener(ControlEvent.SLOW_MOTION_REWIND_DOWN, onSlowMotionRewindDown);
			controlBar.addEventListener(ControlEvent.FAST_FORWARD_DOWN, onFastMotionForwardDown);
			controlBar.addEventListener(ControlEvent.REWIND_DOWN, onFastMotionRewindDown);
			controlBar.addEventListener(ControlEvent.SLOW_MOTION_REWIND_UP, stopTrickMode);
			controlBar.addEventListener(ControlEvent.SLOW_MOTION_FORWARD_UP, stopTrickMode);
			controlBar.addEventListener(ControlEvent.FAST_FORWARD_UP, stopTrickMode);
			controlBar.addEventListener(ControlEvent.REWIND_UP, stopTrickMode);
			controlBar.addEventListener(ControlEvent.MUTE_ON/*ControlEvent.MUTE_CHANGE*/, onMuteChange);//modiy by xys
			controlBar.addEventListener(ControlEvent.MUTE_OFF/*ControlEvent.MUTE_CHANGE*/, onMuteChange);//add by xys
		}
		
		private function onMuteChange(event:ControlEvent):void
		{
			var mute:Boolean = (event.data == ControlEvent.MUTE_ON);
			if(mute)
			{
				restoreVolumeLevel = netStream.volume
			}
			netStream.volume = mute ? 0 : restoreVolumeLevel;
			sharedObject.data.volume = netStream.volume;
		}
		
		override protected function onStreamLength(event:OvpEvent):void
		{
			super.onStreamLength(event)
			netStream.inBufferSeek = true;
			netStream.bufferTime = 15;
			netStream.backBufferTime = streamLength;
		}

		private function onFastMotionRewindDown(event:ControlEvent):void
		{
			startTrickMode(netStream.fastMotionOn, false);
		}
		
		private function onFastMotionForwardDown(event:ControlEvent):void
		{
			startTrickMode(netStream.fastMotionOn);
		}

		private function onSlowMotionRewindDown(event:ControlEvent):void
		{
			startTrickMode(netStream.slowMotionOn, false);
		}
		
		private function onSlowMotionForwardDown(event:ControlEvent):void
		{
			startTrickMode(netStream.slowMotionOn);
		}
		
		private function startTrickMode(trickModeMethod:Function, ... args):void
		{
			netStream.pause();
			playPauseButton.currentState = ControlEvent.PAUSE;
			trickModeMethod.apply(this, args);
			trickModeActive = true;
			netStream.addEventListener(OvpEvent.TRICK_MODE_TIMER_TICK, onTrickTimer)
			if(view.spinnerView.visible)
			{
				onBufferFull();
			}
		}
		
		private function stopTrickMode(event:ControlEvent):void
		{
			if(trickModeActive)
			{
				trickModeActive = false;
				netStream.removeEventListener(OvpEvent.TRICK_MODE_TIMER_TICK, onTrickTimer);
				netStream.trickMotionModeOff();
				onPlayClick();
				playPauseButton.currentState = ControlEvent.PLAY;
			}
		}
		
		private function onTrickTimer(event:OvpEvent):void
		{
			if (netStream.bufferLength < 1 || netStream.backBufferLength < 1)
			{
				stopTrickMode(null);
			}
		}
		
		override protected function onNetStreamStatus(event:NetStatusEvent):void
		{
			switch (event.info.code) 
			{
				case "NetStream.Step.Notify":
					return;
				default :
					if(!trickModeActive)
					{						
						super.onNetStreamStatus(event);
					}
			}
		}
	}
}